/*----------------------------------
CODE FOR FIGURES:
A Registered Report on Place-Based Resentment: Exploring Urban-Rural Tensions in Sweden
------------------------------------*/

clear all // Reset memory and datasets

// Set working directory
cd "/fill in/"

// Open dataset 
use "/Users/ka4012ha/Documents/Research/Papers/Submitted papers/Rural and Urban (CA JEPS)/For OSF/4. Dataverse/2.MainData_RuralResent.dta"


// -------------------------------------------------------------
// Install packages and set graph scheme
// -------------------------------------------------------------
ssc install estout, replace          // Output tables
ssc install fsum                     // Summary stats
ssc install betterbar                 // Improved bar plots
net install schemepack, from("https://raw.githubusercontent.com/asjadnaqvi/stata-schemepack/main/installation/") replace
set scheme cblind1                    // Colorblind-friendly scheme
net install grc1leg, from("http://www.stata.com/users/vwiggins/")
ssc install outreg2                   // Regression output

// -------------------------------------------------------------
// FIGURE 2: Descriptive histograms by Urban/Rural
// -------------------------------------------------------------
twoway (histogram ID_strength if Rural_ID_Bin==1, color(ebblue%70) start(1) width(1) discrete percent) ///
       (histogram ID_strength if Rural_ID_Bin==2, color(orange_red%40) start(1) width(1) discrete percent ///
        xtitle("Place identity strength") title("A. Place identity strength") ///
        legend(order(1 "Urban" 2 "Rural")) xlab(,nogrid) ylab(,nogrid)), name(H1)

twoway (histogram resent if Rural_ID_Bin==1, color(ebblue%70) start(1) width(1) discrete percent) ///
       (histogram resent if Rural_ID_Bin==2, color(orange_red%40) start(1) width(1) discrete percent ///
        xtitle("Place-based resentment") title("B. Place-based resentment") ///
        legend(order(1 "Urban" 2 "Rural")) xlab(,nogrid) ylab(,nogrid)), name(H2)

twoway (histogram PTEI if Rural_ID_Bin==1, color(ebblue%70) start(-6) width(1) discrete percent) ///
       (histogram PTEI if Rural_ID_Bin==2, color(orange_red%40) start(-6) width(1) discrete percent ///
        xtitle("Place-based trait evaluation index") title("C. Place-based trait evaluation index") ///
        legend(order(1 "Urban" 2 "Rural")) xlab(,nogrid) ylab(,nogrid)), name(H3)

graph combine H1 H2 H3 // Combine descriptive plots

// -------------------------------------------------------------
// FIGURE 3: Main results (policy agreement by rural/urban politician)
// -------------------------------------------------------------
betterbar policy if Rural_ID_Bin==2, over(ruralPolitician) vertical ci ///
    barcolor(orange_red%80 orange_red%60) ylab(0(1)7, nogrid) ///
    ytitle("Level of agreement") title("A. Rural respondents") barlab name(Pol1)

betterbar policy if Rural_ID_Bin==1, over(ruralPolitician) vertical ci ///
    barcolor(ebblue%80 ebblue%60) ylab(0(1)7, nogrid) ///
    ytitle("Level of agreement") title("B. Urban respondents") barlab name(Pol2)

graph combine Pol1 Pol2 // Combine main result bars

// -------------------------------------------------------------
// APPENDIX: Robustness checks
// -------------------------------------------------------------
twoway (histogram bigCity if Rural_ID_Bin==1, color(ebblue%70) start(1) width(1) discrete percent) ///
       (histogram bigCity if Rural_ID_Bin==2, color(orange_red%40) start(0) width(1) discrete percent ///
        xtitle("Place of upbringing") title("Robustness: Place of upbringing") ///
        legend(order(1 "Urban" 2 "Rural")) xlab(,nogrid) ylab(,nogrid)), name(City)

twoway (histogram Rural_ID_cont_1 if Rural_ID_Bin==1, color(ebblue%70) start(-50) width(1) discrete percent) ///
       (histogram Rural_ID_cont_1 if Rural_ID_Bin==2, color(orange_red%40) start(-50) width(1) discrete percent ///
        xtitle("Continuous place identity") title("Robustness: continuous place identity") ///
        legend(order(1 "Urban" 2 "Rural")) xlab(,nogrid) ylab(,nogrid)), name(Robust)

graph combine City Robust // Combine robustness histograms

// -------------------------------------------------------------
// APPENDIX: Figures for each policy area (Urban vs Rural)
// -------------------------------------------------------------
* Health policy
betterbar HealthPol if Rural_ID_Bin==2, over(ruralPolitician) vertical ci ///
    barcolor(orange_red%80 orange_red%60) ylab(0(1)7, nogrid) ///
    ytitle("Level of agreement") title("Rural respondents") barlab name(HealthRur)

betterbar HealthPol if Rural_ID_Bin==1, over(ruralPolitician) vertical ci ///
    barcolor(ebblue%80 ebblue%60) ylab(0(1)7, nogrid) ///
    ytitle("Level of agreement") title("Urban respondents") barlab name(HealthUrb)

graph combine HealthRur HealthUrb, name(policy1) title("A. Health")

* Education policy
betterbar EducPol if Rural_ID_Bin==2, over(ruralPolitician) vertical ci ///
    barcolor(orange_red%80 orange_red%60) ylab(0(1)7, nogrid) ///
    ytitle("Level of agreement") title("Rural respondents") barlab name(EducRur)

betterbar EducPol if Rural_ID_Bin==1, over(ruralPolitician) vertical ci ///
    barcolor(ebblue%80 ebblue%60) ylab(0(1)7, nogrid) ///
    ytitle("Level of agreement") title("Urban respondents") barlab name(EducUrb)

graph combine EducRur EducUrb, name(policy2) title("B. Education")

* Immigration policy
betterbar ImmigPol if Rural_ID_Bin==2, over(ruralPolitician) vertical ci ///
    barcolor(orange_red%80 orange_red%60) ylab(0(1)7, nogrid) ///
    ytitle("Level of agreement") title("Rural respondents") barlab name(immigRur)

betterbar ImmigPol if Rural_ID_Bin==1, over(ruralPolitician) vertical ci ///
    barcolor(ebblue%80 ebblue%60) ylab(0(1)7, nogrid) ///
    ytitle("Level of agreement") title("Urban respondents") barlab name(immigUrb)

graph combine immigRur immigUrb, name(policy3) title("C. Immigration")

graph combine policy1 policy2 policy3, rows(3) // Stack policy results

// -------------------------------------------------------------
// APPENDIX: Pooled policy agreement (all respondents)
// -------------------------------------------------------------
betterbar policy, over(ruralPolitician) vertical ci ///
    barcolor(grey%60 grey%20) ylab(0(1)7, nogrid) ///
    ytitle("Level of agreement") title("A. Level of agreement") barlab name(poolAll)

betterbar HealthPol, over(ruralPolitician) vertical ci ///
    barcolor(grey%60 grey%20) ylab(0(1)7, nogrid) ///
    ytitle("Level of agreement") title("B. Health") barlab name(p1)

betterbar EducPol, over(ruralPolitician) vertical ci ///
    barcolor(grey%60 grey%20) ylab(0(1)7, nogrid) ///
    ytitle("Level of agreement") title("C. Education") barlab name(p2)

betterbar ImmigPol, over(ruralPolitician) vertical ci ///
    barcolor(grey%60 grey%20) ylab(0(1)7, nogrid) ///
    ytitle("Level of agreement") title("C. Immigration") barlab name(p3)

graph combine poolAll p1 p2 p3, row(2) // Combine pooled results
